package com.powernode.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.ProdProp;
import com.powernode.domain.ProdPropValue;
import com.powernode.service.ProdPropService;
import com.powernode.service.ProdPropValueService;
import com.powernode.utils.AuthUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 内容:
 * 作者: cy
 */
@RestController
@Api(tags = "属性规格管理")
@RequestMapping("prod/spec")
public class PropController {

    @Autowired
    private ProdPropService prodPropService;

    @Autowired
    private ProdPropValueService prodPropValueService;

    @GetMapping("page")
    @ApiOperation("分页查询属性列表")
    @PreAuthorize("hasAuthority('prod:spec:page')")
    public ResponseEntity<Page<ProdProp>> loadProdPropPage(Page<ProdProp> page, ProdProp prodProp) {
        Page<ProdProp> prodPropPage = prodPropService.selectProdPropPage(page, prodProp);
        return ResponseEntity.ok(prodPropPage);
    }

    @PostMapping
    @ApiOperation("新增属性和属性值")
    @PreAuthorize("hasAuthority('prod:spec:save')")
    public ResponseEntity<Void> addProdProp(@RequestBody ProdProp prodProp) {
        Long shopId = AuthUtil.getUserIdOrShopId(false);
        prodProp.setShopId(shopId);
        prodPropService.savePropProdAndValues(prodProp);
        return ResponseEntity.ok().build();
    }


    @GetMapping("list")
    @ApiOperation("查询当前店铺的所有属性集合")
    @PreAuthorize("hasAuthority('prod:spec:page')")
    public ResponseEntity<List<ProdProp>> loadShopProdProp() {
        Long shopId = AuthUtil.getUserIdOrShopId(false);
        List<ProdProp> prodPropList = prodPropService.selectShopProdProp(shopId);
        return ResponseEntity.ok(prodPropList);
    }


    @GetMapping("listSpecValue/{propId}")
    @ApiOperation("根据属性的id查询属性值的集合")
    @PreAuthorize("hasAuthority('prod:spec:page')")
    public ResponseEntity<List<ProdPropValue>> loadProdPropValueByPropId(@PathVariable("propId") Long propId) {
        List<ProdPropValue> propValueList = prodPropValueService.selectProdPropValueByPropId(propId);
        return ResponseEntity.ok(propValueList);
    }
}
